<template>
  <section>
    <!--工具条
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item style="float:right;">
          <el-button v-show="exportBtnVisible" size="small" class="button_style" type="primary" icon="upload2" @click="exportExcels">导出</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    -->

    <!--产品销量排名列表-->
    <el-table :data="produntsCount" stripe border :height="winHeight-120" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column prop="userId" label="用户ID"></el-table-column>
      <el-table-column prop="userName" label="用户姓名"></el-table-column>
      <el-table-column prop="productName" label="产品名称"></el-table-column>
      <el-table-column prop="count" label="购买量"></el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination style="float:right" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[10, 20, 40, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-col>

  </section>
</template>

<script>
import util from '../../common/js/util'
import commons from '../../common/js/common'
import { mapState, mapActions } from 'vuex'
import { myProductCountList } from '../../api/api';

export default {
  data() {
    return {
      filters: {
      },
      produntsCount: [],
      lists: [],
      exportTotal: 0,
      total: 0,
      loading: '',
      listLoading: false,
      sels: [],//列表选中列
      page: 1,
      pageSize: 10,
      IsShow: false,

      queryBtnVisible: false,
      exportBtnVisible: false
    }
  },
  computed: mapState([
    'winWidth',
    'winHeight'
  ]),
  methods: {
    handleCurrentChange(val) {
      this.page = val;
      this.getMyCountProdectDate(true);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getMyCountProdectDate(true);
    },
    //获取列表
    getMyCountProdectDate(bool) {
      let para = new FormData();
      para.append("pageSize", bool ? this.pageSize : this.exportTotal);
      para.append("pageNumber", bool ? this.page : 1);
      this.listLoading = bool;
      if (!bool) {
        this.loading = this.$loading({
          lock: true,
          text: '正在导出',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      }
      myProductCountList(para).then((res) => {
        console.log(res);
        this.lists = res.data.result.list;
        this.total = res.data.result.total;
        this.exportTotal = res.data.result.total;
        this.listLoading = false;
        if (!bool) {
          this.export2Excel();
          this.loading.close();
        }
        else {
          this.produntsCount = res.data.result.list;
        }
      });
    },
    //table导出excel
    exportExcels: function () {
      this.getMyCountProdectDate(false);
    },
    export2Excel: function () {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../vendor/Export2Excel');
        const tHeader = ['标题头'];
        const filterVal = ['标题头英文'];
        const list = this.lists;
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '日志列表excel');
      })
    },
    formatJson: function (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },

    selsChange: function (sels) {
      this.sels = sels;
    },

    btnShow: function () {
      let thisData = JSON.parse(sessionStorage.getItem('144'));
      let _this = this;
      $.each(thisData, function (i, j) {
        if (thisData[i] == "systemLogQuery") {
          _this.queryBtnVisible = true;
        }
        if (thisData[i] == "systemLogExport") {
          _this.exportBtnVisible = true;
        }
      });
    }
  },
  mounted() {
    this.getMyCountProdectDate(true);
    this.btnShow();
  }
}
</script>